package com.example.webcamtest;

import org.vaadin.webcamforvaadin.WebCamForVaadin;
import org.vaadin.webcamforvaadin.WebCamListener;

import com.github.wolfie.refresher.Refresher;
import com.vaadin.Application;
import com.vaadin.terminal.StreamResource;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Window;

public class WebcamtestApplication extends Application implements
		WebCamListener {
	private static final long serialVersionUID = 8604097340045749189L;
	private Window mainWindow;
	private WebCamForVaadin webcam;
	private Refresher refresher = new Refresher();
	private Embedded image;

	@Override
	public void init() {
		refresher.setRefreshInterval(1000);
		mainWindow = new Window("WebcamApplication");
		mainWindow.addComponent(refresher);
		webcam = new WebCamForVaadin(this);
		mainWindow.addComponent(webcam);
		image = new Embedded("Image from webcam", null);
		mainWindow.addComponent(webcam);
		mainWindow.addComponent(image);
		setMainWindow(mainWindow);
	}

	public void onImageReady() {
		StreamResource picture = webcam.getPicture();
		picture.setMIMEType("image/jpeg");
		image.setImmediate(true);
		image.setMimeType("image/jpeg");
		image.setSource(picture);
		mainWindow.requestRepaint();
	}

	public void onError(String errorMessage) {
	}
}
